//
//  TheNewSongsViewController.m
//  NewMusic
//
//  Created by dllo on 15/11/14.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "TheNewSongsViewController.h"
#import "CustomerNetworking.h"
#import "MoreNewSongsModel.h"
#import "TheNewMusicCollectionViewCell.h"
#import "TheNewCollectionReusableView.h"
#import "HotListenListViewController.h"
#import <MJRefresh.h>
@interface TheNewSongsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, assign) BOOL loading;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger totalPage;
@property (nonatomic, retain) NSMutableArray *totalDataArray;
@end

@implementation TheNewSongsViewController
- (void)dealloc {
    [_collectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"最新音乐";
    self.totalDataArray = [NSMutableArray array];
    self.page = 1;
    //创建button
    [self createButton];
    //创建collectionView
    [self createCollectionView];
    //获取数据
    [self loadData];
    //刷新数据
    [self requireNewData];
}
#pragma mark 创建button
- (void)createButton {
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
}
- (void)buttonAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)requireNewData {
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.loading = YES;
        self.page = 1;
        [self loadData];
    }];
    self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.loading = NO;
        self.page ++;
        if (self.page > self.totalPage) {
            [self.collectionView.footer noticeNoMoreData];
        }else {
            [self loadData];
        }
        
    }];
}

#pragma mark 获取数据
- (void)loadData {
    [CustomerNetworking customerConnectWithURL:[NSString stringWithFormat: @"http://api.dongting.com/misc/album/new?page=%ld&size=30&app=ttpod&v=v8.1.1.2015110616", self.page] andParameters:nil andBlock:^(id response) {
        if (self.loading == YES) {
            [self.totalDataArray removeAllObjects];
        }
        self.totalPage = [[response valueForKey:@"pageCount"] integerValue];
        NSArray *array = [response valueForKey:@"data"];
        if (array.count != 0) {
            NSMutableArray *dataArray = [NSMutableArray array];
            for (NSDictionary *dic in array) {
                MoreNewSongsModel *model = [[MoreNewSongsModel alloc]initWithDictionary:dic];
                [dataArray addObject:model];
                [model release];
            }
            [self.totalDataArray addObject:dataArray];
        }
        [self.collectionView.header endRefreshing];
        [self.collectionView.footer endRefreshing];
        [_collectionView reloadData];
    }];
}
#pragma mark 创建collectionView
- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    //注册单元格重用标识
    [_collectionView registerClass:[TheNewMusicCollectionViewCell class] forCellWithReuseIdentifier:@"TheNewMusicCollectionViewCellIdentify"];
    //注册头视图
    [_collectionView registerClass:[TheNewCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.view addSubview:_collectionView];
    [flowLayout release];
    [_collectionView release];
}
#pragma mark cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TheNewMusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TheNewMusicCollectionViewCellIdentify" forIndexPath:indexPath];
    if (self.totalDataArray.count != 0 && [self.totalDataArray[indexPath.section] count] != 0) {
        cell.moreNewSongsModel = [self.totalDataArray[indexPath.section] objectAtIndex:indexPath.item];
    }
    return cell;
}
#pragma mark 头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    TheNewCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    if (self.totalDataArray.count != 0 && [self.totalDataArray[indexPath.section] count] != 0) {
        header.moreNewSongsModel = [self.totalDataArray[indexPath.section] objectAtIndex:0];
    }
    return header;
}
#pragma mark cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((self.view.frame.size.width - 30) / 2, self.view.frame.size.height / 3);
}
#pragma mark 头视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(self.view.frame.size.width, 40);
}
#pragma mark cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.totalDataArray.count != 0 && [self.totalDataArray[section] count] != 0) {
        return [self.totalDataArray[section] count];
    }else {
        return 0;
    }
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _totalDataArray.count ? _totalDataArray.count : 0;
}
#pragma mark 最小的cell的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 2;
}
#pragma mark 最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}
#pragma mark cell距离四周的间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 10, 0, 10);
}
#pragma mark cell的点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HotListenListViewController *view = [[HotListenListViewController alloc]init];
    view.idString = [[self.totalDataArray[indexPath.section] objectAtIndex:indexPath.row] ID];
    view.numberString = @"http://api.dongting.com/song/album/";
    [self.navigationController pushViewController:view animated:YES];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [view release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
